<template>
  <div v-loading="loading" class="xk857-container">
    <!-- 条件查询 -->
    <el-form :inline="true" :model="query" size="mini">
      <el-form-item label="分类名称：">
        <el-input v-model.trim="query.name" />
      </el-form-item>
      <el-form-item label="状态：">
        <el-select v-model="query.status" clearable filterable style="width: 85px">
          <el-option v-for="item in statusOptions" :key="item.code" :label="item.name" :value="item.code" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
        <el-button icon="el-icon-refresh" @click="reload">重置</el-button>
        <el-button icon="el-icon-circle-plus-outline" type="primary" @click="openAdd">新增</el-button>
      </el-form-item>
    </el-form>

    <!-- 核心列表 -->
    <el-table :data="categoryList" stripe border style="width: 100%">
      <el-table-column align="center" type="index" label="序号" width="60" />
      <el-table-column align="center" prop="name" label="分类名称" />
      <el-table-column align="center" prop="sort" width="100" label="排序" />
      <el-table-column align="center" prop="remark" label="备注" />

      <el-table-column align="center" prop="status" width="100" label="状态">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status | statusFilter">
            {{ scope.row.status ? '正常' : '禁用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button type="danger" size="mini" :loading="deleteLoading" @click="showDeleteDialog(scope.row.id)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="query.current"
      :page-sizes="[10, 20, 50]"
      :page-size="query.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="query.total" >
    </el-pagination>

    <edit :title="edit.title" :visible="edit.visible" :formData="edit.formData" :remoteClose="remoteClose"/>

    <!-- 删除确认对话框 -->
    <el-dialog
      title="警告"
      :visible.sync="dialogVisible"
      width="30%">
      <span>您确定要删除此模块吗</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="danger" @click="handleDelete(deleteId)" >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import api from '@/api/categorys'
import Edit from '@/views/category/edit'

const statusOptions = [
  {code: 0, name: '禁用'},
  {code: 1, name: '正常'}
]
export default {

  name: 'Category',
  components: {
    Edit // Edit: Edit
  },
  data() {
    return {
      categoryList: [], // 列表数据
      query: {
        current: 1,
        size: 20,
        title: 0,
        name: '',
        status: ''
      },
      statusOptions,
      edit: {
        title: '',
        visible: false,
        formData: {}
      },
      loading: false, // 整个页面加载
      deleteLoading: false, // 删除按钮加载
      deleteId: '', // 删除的id
      dialogVisible: false
    }
  },
  created() {
    this.fetchData()
  },
  filters: {
    statusFilter(status) {
      // 0 是禁用， 1是正常
      const statusMap = {0: 'danger', 1: 'success'}
      return statusMap[status]
    }
  },
  methods: {
    // 获取列表数据
    fetchData() {
      this.loading = true
      api.getList(this.query).then(res => {
        if (res.code === 20000) {
          this.categoryList = res.data.records
          this.query.current = res.data.current
          this.query.size = res.data.size
          this.query.total = res.data.total
        } else {
          this.$message.error(res.message)
        }
        this.loading = false
      }).catch(e => {
        this.loading = false
        this.$message.error('请检查您的网络连接后重试')
      })
    },
    // 编辑
    handleEdit(id) {
      this.loading = true
      // 通过id查询详情
      api.getById(id).then(response => {
        if (response.code === 20000) {
          // 将查询的详情传递
          this.edit.formData = response.data
          this.edit.title = '编辑'
          this.edit.visible = true
        }
        this.loading = false
      }).catch(e => {
        this.loading = false
        this.$message.error('请检查您的网络连接后重试')
      })
    },
    showDeleteDialog(id) {
      this.deleteId = id
      this.dialogVisible = true
    },
    // 删除
    handleDelete(id) {
      api.deleteById(id).then(response => {
        this.dialogVisible = false
        // 处理响应结果提示
        this.$message({
          type: response.code === 20000 ? 'success' : 'error',
          message: response.message
        })
      }).catch(e => {
        this.dialogVisible = false
      })
      this.dialogVisible = false
      // 刷新列表数据
      this.fetchData()
    },
    // 查询按钮
    queryData() {
      // 将页码变为1，第1页
      this.query.current = 1
      this.fetchData()
    },
    // 新增按钮
    openAdd() {
      // 打开新增按钮
      this.edit.visible = true
      this.edit.title = '新增模块'
    },
    // 重置
    reload() {
      this.query = {}
      this.fetchData()
    },
    // 子组件会触发此事件方法来关闭窗口
    remoteClose() {
      this.edit.formData = {}
      this.edit.visible = false
      this.fetchData()
    },
    // val 是切换之后的每页显示多少条
    handleSizeChange(val) {
      this.query.size = val
      this.fetchData()
    },

    // 当页码改变后触发到此方法，val 是当前点击（或输入）的那个页码，
    handleCurrentChange(val) {
      this.query.current = val
      this.fetchData()
    }
  }
}
</script>

<style scoped>

</style>
